package engine;

import java.awt.Graphics2D;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.background.ImageBackground;
import com.golden.gamedev.object.PlayField;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.Timer;
import resources.manager.ResourceManager;
import sprites.*;

/**
 * This class will be used to merge ProgBob and BubblefishBob. It will
 * eventually be renamed BubblefishBob, and it will contain the code for the
 * game (in a much better way).
 */
public class BubblefishBobGame extends GameObject
{

    /*
     * The field holds all game objects.
     */

    private String myString;

    private PlayField field;

    private Background bg;

    private Random rng;
    private int myLevel;

    private SpriteGroup BOB, BUBBLEFIRED, BUBBLEPATH, BONUSES, ITEMS, EVILFISH;

    /*
     * Bob is the fish that the player controls. BubbleFired are the bubbles
     * that Bob fires; BubblePath are the bubbles that are floating on the
     * path (can have fish in them or not). Bonuses are powerups that drop down
     * to Bob. Items are special weapons that Bob can use, like torpedos and
     * small rocks. Finally, EvilFish is the evil fish that eats bubbles.
     */
    private Bob bob;

    private EmptyBubble firebubble;

    private BubblefishBobSprite myEvilFish;

    private ArrayList<Point> myPoints;

    private BufferedImage[] myEmptyBubbles;

    /*
     * The fire timer controls the firing rate of the player. The spawn timer
     * controls how fast new bubbles spawn on the path.
     */
    private Timer fireTimer, spawnTimer;

    public BubblefishBobGame(GameEngine parent, int level) {
        super(parent);
        ResourceManager manager = ResourceManager.getInstance();
        manager.addResourcesFromFile("alllevels");
        myLevel = level;
        myString = "level" + level;
        myPoints = new ArrayList<Point>();
        myEmptyBubbles = manager.getBufferedImageArray("emptybubbles");
    }


    @Override
    public void initResources()
    {
        rng = new Random(myEmptyBubbles.length);


        bg = new ImageBackground(getImage("resources/images/seagrass.jpg"));
        field = new PlayField(bg);

        BOB = field.addGroup(new SpriteGroup("Bob"));
        BUBBLEFIRED = field.addGroup(new SpriteGroup("Bubbles fired"));
        BUBBLEPATH = field.addGroup(new SpriteGroup("Bubbles on path"));
        BONUSES = field.addGroup(new SpriteGroup("Bonuses"));
        ITEMS = field.addGroup(new SpriteGroup("Items"));
        EVILFISH = field.addGroup(new SpriteGroup("Evil Fish"));

        /*
         * Load level data and add path points.
         */
        Level l = new Level(myString);
        myPoints = l.getPoints();

        for(int i = 0; i<myPoints.size()-1; i++){
            double x = myPoints.get(i).x;
            double y = myPoints.get(i).y;
            BUBBLEPATH.add(new PathBubble(this, x, y));
        }

        /*
         * Add Bob to the screen and make him belong to the BOB group. (NB: it
         * doesn't matter where you spawn Bob, since his x coordinates are
         * determined by the mouse and his y coordinates are fixed.)
         */
        bob = new Bob(this, 200, 200);
        BOB.add(bob);

        /*
         * Summon the evil fish to the correct position. All the funky number
         * manipulation is required to compensate for ProgBob quirks; do not
         * ask questions.
         */
        double evilx = l.getLast().x - 10;
        double evily = l.getLast().y - 30;
        myEvilFish = new EvilFish(this, evilx, evily);
        EVILFISH.add(myEvilFish);

        /*
        ArrayList<BufferedImage> temp = new ArrayList<BufferedImage>();
        for(BufferedImage i: myEmptyBubbles){
            temp.add(i);
        }
        Collections.shuffle(temp);
        temp.toArray(myEmptyBubbles);
        firebubble=new EmptyBubble(this,myEmptyBubbles,bob.getX(),bob.getY());
        BUBBLEFIRED.add(firebubble);
         */

    }

    @Override
    public void update(long elapsedTime)
    {

        field.update(elapsedTime);

        /*
         * Exit if the player hits escape.
         */
        if(keyPressed(KeyEvent.VK_ESCAPE)) finish();

        /*
         * DEBUG: pressing 'V' lets you go on to the next level
         */
        if(keyPressed(KeyEvent.VK_V))
        {
            parent.nextGameID=2;
            finish();
        }

        /*
         * Clicking makes Bob fire projectiles.
         */
        if(click())
        {
            Projectile p = bob.fire();
            p.setVerticalSpeed(-0.2);
            BUBBLEFIRED.add(p);
        }
    }

    @Override
    public void render(Graphics2D g)
    {
        field.render(g);

    }

}
